package com.neudu.ssrs_yyzxservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.ssrs_yyzxservice.common.JSONReturn;
import com.neudu.ssrs_yyzxservice.pojo.Backdown;
import com.neudu.ssrs_yyzxservice.pojo.Customer;
import com.neudu.ssrs_yyzxservice.service.BackdownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BackdownController {

    @Autowired
    private BackdownService backdownService;
    @Autowired
    private JSONReturn jsonReturn;

    @RequestMapping("get_all_backdowns")
    public String getAll(@RequestBody PageInfo pageInfo){
        try {
            PageInfo<Backdown> pageInfoReturn = backdownService.getAllBackdowns(pageInfo.getPageNum(), pageInfo.getPageSize());
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }


    @RequestMapping("get_backdowns_by_customer_id")
    public String getByCustomerId(@RequestParam("customer_id") int customer_id){
        try {
            Backdown backdown = backdownService.getBackdownById(customer_id);
            return jsonReturn.returnSuccess(backdown);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }


    @RequestMapping("add_one_backdown")
    public String addOne(@RequestBody Backdown backdown){
        try {
            backdownService.addBackdown(backdown);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_backdown_by_id")
    public String getById(@RequestParam("id") Integer id){
        try {
            Backdown backdown = backdownService.getBackdownById(id);
            return jsonReturn.returnSuccess(backdown);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modify_backdown_by_id")
    public String modifyById(@RequestBody Backdown backdown){
        try {
            backdownService.updateBackdown(backdown);
            return jsonReturn.returnSuccess(backdown);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("remove_backdown_by_id")
    public String removeById(@RequestParam("id") Integer id){
        try {
            backdownService.deleteBackdownById(id);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}